package com.clip.action;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.clip.entity.csch.City;
import com.clip.entity.csch.Department;
import com.clip.entity.csch.District;
import com.clip.entity.csch.Province;
import com.clip.entity.csch.School;
import com.clip.service.CSCHService;
import com.clip.service.UserService;
import com.clip.util.Struts2Utils;
import com.clip.util.ToolsUtils;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@Scope("prototype")
public class SearchAction extends ActionSupport{
	@Autowired
	private CSCHService cschService;
	@Autowired
	private UserService userService;
	public void getProvinces(){
		List<Province> list=cschService.getAllProvinces();
		String str=provincesToXml(list);
		Struts2Utils.renderXml(str);
	}
	
	public void getCities(){
		List<City> list=cschService.getCitiesByPid(pid);
		String str=citiesToXml(list);
		Struts2Utils.renderXml(str);
	}

	public void getDistricts(){
		List<District> list=cschService.getDistrictsBypidppid(pid,ppid);
		String str=districtsToXml(list);
		Struts2Utils.renderXml(str);
	}
	
	public void getSchools(){
		List list=cschService.getSchoolsBytypeANDpid(schooltype,pid);
		String str=schoolsToXml(list);
		Struts2Utils.renderXml(str);
	}
	
	public void getDepartments(){
		List list=cschService.getDepartmentsBypid(pid);
		String str=departmentsToXml(list);
		Struts2Utils.renderXml(str);
	}
	
	public void searchUserByCareer(){
		
	}
	
	public void searchUserByEducation(){
		
	}
	
	private String departmentsToXml(List list){
		Map<String, Map> returnMap=new LinkedHashMap<String, Map>();
		for (Object object : list) {
			Department deparement=(Department) object;
			Map<String,Object> temp2=new HashMap<String,Object>();
			temp2.put("name", deparement.getName());
			temp2.put("pid", deparement.getPid());
			returnMap.put("Department id="+deparement.getId(), temp2);
		}
		return ToolsUtils.getDOM(returnMap);
	}
	
	private String schoolsToXml(List list) {
		Map<String, Map> returnMap=new LinkedHashMap<String, Map>();
		for (Object object : list) {
			School school=(School) object;
			Map<String,Object> temp2=new HashMap<String,Object>();
			temp2.put("name", school.getName());
			temp2.put("pid", school.getPid());
			returnMap.put("School id="+school.getId(), temp2);
		}
		return ToolsUtils.getDOM(returnMap);
	}

	private String districtsToXml(List<District> list) {
		Map<String, Map> returnMap=new LinkedHashMap<String, Map>();
		for (District district : list) {
			Map<String,Object> temp2=new HashMap<String,Object>();
			temp2.put("name", district.getName());
			temp2.put("type", district.getType());
			temp2.put("pid", district.getPid());
			temp2.put("ppid", district.getPpid());
			returnMap.put("District id="+district.getId(), temp2);
		}
		return ToolsUtils.getDOM(returnMap);
	}

	private String citiesToXml(List<City> list) {
		Map<String, Map> returnMap=new LinkedHashMap<String, Map>();
		for (City city : list) {
			Map<String,Object> temp2=new HashMap<String,Object>();
			temp2.put("name", city.getName());
			temp2.put("type", city.getType());
			temp2.put("pid", city.getPid());
			returnMap.put("City id="+city.getId(), temp2);
		}
		return ToolsUtils.getDOM(returnMap);
	}
	
	private String provincesToXml(List<Province> list) {
		Map<String, Map> returnMap=new LinkedHashMap<String, Map>();
		for (Province province : list) {
			Map<String,Object> temp2=new HashMap<String,Object>();
			temp2.put("name", province.getName());
			temp2.put("type", province.getType());
			returnMap.put("Province id="+province.getId(), temp2);
		}
		return ToolsUtils.getDOM(returnMap);
	}
	private String schooltype;
	private int pid;
	private int ppid;
	private String nameInput;
	private String companyInput;
	private String schoolInput;
	private int loadtype;
	private int schoolid;
	private String dpname;
	
	public String getSchooltype() {
		return schooltype;
	}
	public void setSchooltype(String schooltype) {
		this.schooltype = schooltype;
	}

	public int getPid() {
		return pid;
	}

	public void setPid(int pid) {
		this.pid = pid;
	}

	public int getPpid() {
		return ppid;
	}

	public void setPpid(int ppid) {
		this.ppid = ppid;
	}

	public String getNameInput() {
		return nameInput;
	}

	public void setNameInput(String nameInput) {
		this.nameInput = nameInput;
	}

	public String getCompanyInput() {
		return companyInput;
	}

	public void setCompanyInput(String companyInput) {
		this.companyInput = companyInput;
	}

	public String getSchoolInput() {
		return schoolInput;
	}

	public void setSchoolInput(String schoolInput) {
		this.schoolInput = schoolInput;
	}

	public int getLoadtype() {
		return loadtype;
	}

	public void setLoadtype(int loadtype) {
		this.loadtype = loadtype;
	}

	public int getSchoolid() {
		return schoolid;
	}

	public void setSchoolid(int schoolid) {
		this.schoolid = schoolid;
	}

	public String getDpname() {
		return dpname;
	}

	public void setDpname(String dpname) {
		this.dpname = dpname;
	}
	
	
}
